package dataStructure.linkedlist;

public class DoubleLinkedListDemo {
    public static void main(String[] args) {
        DoubleLinkedList doubleLinkedList = new DoubleLinkedList();
        HeroNodePlus hero1 = new HeroNodePlus(1, "宋江", "及时雨");
        HeroNodePlus hero2 = new HeroNodePlus(2, "卢俊义", "玉麒麟");
        HeroNodePlus hero3 = new HeroNodePlus(3, "吴用", "智多星");
        HeroNodePlus hero4 = new HeroNodePlus(4, "林冲", "豹子头");

//        doubleLinkedList.add(hero1);
//        doubleLinkedList.add(hero2);
//        doubleLinkedList.add(hero4);
//        doubleLinkedList.add(hero3);
//        doubleLinkedList.print();

        doubleLinkedList.addByOrder(hero1);
        doubleLinkedList.addByOrder(hero2);
        doubleLinkedList.addByOrder(hero4);
        doubleLinkedList.addByOrder(hero3);
        doubleLinkedList.print();
        System.out.println("---------------------------");
        doubleLinkedList.delete(3);
        doubleLinkedList.print();
    }
}

class DoubleLinkedList{
    private HeroNodePlus head = new HeroNodePlus(0,"","");  //头结点

    public HeroNodePlus getHead() {
        return head;
    }

    public void add(HeroNodePlus heroNode){
        HeroNodePlus temp = head;   //指向第一个节点
        while (true){
            //temp.next==null 表示指针指向了最后一个结点，最后一个结点的next一定为null
            //temp = null  表示指针指向了最后一个结点的后一个，
            if (temp.next==null){
                break;
            }
            temp = temp.next;
        }
        temp.next = heroNode;
        heroNode.pre = temp;
    }
    public void delete(int no){
        if (head.next==null){
            System.out.println("链表为空");
        }
        HeroNodePlus temp = head.next;  //指向第一个节点 ，因为上面已经判断不为空了
        boolean flag = false;
        while (true){
            if (temp==null){
                break;
            }
            if (temp.no==no){
                flag = true;
                break;
            }

            temp = temp.next;
        }
        if (flag){
            //被删除的节点会被垃圾回收机制回收
            temp.pre.next = temp.next;
            //判断该结点是否是最后一个
            if (temp.next!=null){
                temp.next.pre = temp.pre;
            }

        }else {
            System.out.println("没有该编号");
        }
    }

    public void update(HeroNodePlus heroNode){
        if (head.next==null){
            System.out.println("链表为空");
        }

        HeroNodePlus temp = head.next;
        boolean flag = false;
        while (true){


            if (temp.no == heroNode.no){
                flag = true;
                break;
            }
            if (temp.next==null){
                break;     //链表全部遍历完
            }
            temp = temp.next;
        }
        if (flag){
            temp.name=heroNode.name;
            temp.nickname=heroNode.nickname;
        }else {
            System.out.println("没有找到");
        }
    }


    public void addByOrder(HeroNodePlus heroNode){
        HeroNodePlus temp = head;
        boolean flag = false;   //定义标志，如果有重复编号，插入失败
        while (true){
            if (temp.next==null){
                break;
            }
            if (temp.no==heroNode.no){
                flag = true;
                break;
            }
            if (temp.no>heroNode.no){
                break;
            }
            temp = temp.next;
        }
        if (flag){
            //说明添加重复
            System.out.printf("序号%d已存在\n",heroNode.no);
        }else {
//            heroNode.next = temp.next;
//            temp.next = heroNode;


            temp.next = heroNode;
            heroNode.pre = temp;

        }

    }
    public void print(){
        if (head.next==null){
            System.out.println("链表为空\n");
            return;
        }
        HeroNodePlus temp = head.next;
        while (true){
            if (temp==null){
                break;
            }
            System.out.println(temp);
            temp = temp.next;
        }

    }
}

class HeroNodePlus{
    public int no;
    public String name;
    public String nickname;
    public HeroNodePlus next; //指向下一个节点
    public HeroNodePlus pre;

    HeroNodePlus(int no,String name,String nickname){
        this.name=name;
        this.nickname=nickname;
        this.no=no;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickname='" + nickname + '\'' +
                '}';
    }


}
